package aom.ju.ss.utils;

import java.text.DecimalFormat;

public class LocationUtils {



    /**
     * 根据两点的经纬度 计算距离
     */
    private static final double EARTH_RADIUS = 6378137.0;

    /**
     * 计算距离 返回单位km
     * @param longitude
     * @param latitue
     * @param longitude2
     * @param latitue2
     * @return
     */
    public static double getDistance(double longitude, double latitue, double longitude2, double latitue2) {
        double lat1 = rad(latitue);
        double lat2 = rad(latitue2);
        double a = lat1 - lat2;
        double b = rad(longitude) - rad(longitude2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }



    /**
     * 距离只保留两位小数
     * @param distance 以米为单位
     * @return
     */
    public static String distanceFormat(double distance) {
        String str;
        double value = distance;
        if (distance >= 1000) {
            value = value / 1000;
            str = "公里";
        } else {
            str = "米";
        }
        return String.format("%s%s", formatDecimal(value), str);
    }

    /**
     * 保留小数点 后两位："#.00"
     * @param format
     * @return
     */
    public static String formatDecimal(double format) {
        DecimalFormat df = new DecimalFormat("##");
        System.out.println(df.format(format));
        return df.format(format);
    }

}
